<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Site extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->title = "SMILE | Selamat datang, telkom siap melayani Anda!";
        $this->layout = "admin";
    }

    /* PAGE-RELATED FUNCTIONS */

    public function index() {
        if ($this->session->userdata('is_super_admin')) {
            redirect('admin/site/question_management');
        } else if ($this->session->userdata('is_admin')) {
            redirect('admin/useradmin');
        } else if ($this->session->userdata('is_pic')) {
            redirect('admin/statistic');
        } else {
            $this->load->view('admin/admin_login');
        }
    }

    public function dummy() {
        $this->layout = false;
        $param = array(6, 5, 3);
        echo $this->respondmodel->get_year_available($param);
    }

    public function login() {
        if ($this->session->userdata('is_super_admin')) {
            redirect('admin/site/question_management');
        } else if ($this->session->userdata('is_admin')) {
            redirect('admin/useradmin');
        } else if ($this->session->userdata('is_pic')) {
            redirect('admin/statistic');
        } else {
            $this->load->view('admin/admin_login');
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect('/admin/');
    }

    public function question_management() {
        if ($this->session->userdata("is_super_admin")) {
            $menudata['is_admin'] = $this->session->userdata('is_admin');
            $menudata['is_pic'] = $this->session->userdata('is_pic');
            $menudata['is_super_admin'] = $this->session->userdata('is_super_admin');
            $this->menubar = $this->load->view('admin/menubar', $menudata);

            $param['question_all'] = $this->get_all_question();
            $param['question_all_visible'] = $this->get_all_visible_question();
            $this->load->view('admin/question_management', $param);
        } else if ($this->session->userdata("is_admin")) {
            redirect("admin/useradmin");
        } else if ($this->session->userdata("is_pic")) {
            redirect("admin/statistic");
        } else {
            redirect("admin/site");
        }
    }

    /* END OF PAGE-RELATED FUNCTIONS */

    /* OPERATION-RELATED FUNCTIONS */

    public function get_all_question() {
        return $this->questionmodel->get_all_question(null, 'question_id', 10000, 0);
    }

    public function get_all_visible_question() {
        return $this->questionmodel->get_all_visible_question('parent_id', 10000, 0);
    }

    /* END OF OPERATION-RELATED FUNCTIONS */

    /* AJAX-RELATED FUNCTIONS */

    public function user_login() {
        // handling user login
        $this->layout = false;
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        if ($this->usermodel->is_login_correct($username, trim($password))) {
            // is correct
            $role = $this->usermodel->get_user_role($username);

            if ($role == 0) {
                // is_superadmin
                $this->session->set_userdata('is_super_admin', 1);
            } else if ($role == 1) {
                // is_admin
                $plasa_id = $this->usermodel->get_plasa_id_by_username($username);
                $this->session->set_userdata('is_admin', $plasa_id);
            } else if ($role == 2) {
                // is_pic
                $plasa_id = $this->usermodel->get_plasa_id_by_username($username);
                $this->session->set_userdata('is_pic', $plasa_id);
            }
            $this->session->set_userdata('username', $username);

            echo "succ";
        } else {
            // incorrect
            echo "err";
        }
    }

    public function add_question() {
        // adding question
        $this->layout = false;
        $question_content = $this->input->post('question_content');
        $this->questionmodel->add_question(-99, 1, $question_content);
        echo 'succ';
    }

    public function delete_question() {
        // delete question
        $this->layout = false;
        $question_id = $this->input->post('question_id');
        $this->questionmodel->delete_question($question_id);
        echo 'succ';
    }

    public function update_question() {
        //updating question
        $this->layout = false;
        $question_id = $this->input->post('question_id');
        $question_content = $this->input->post('question_content');

        $num_rows = $this->respondmodel->get_all_respond_by_questionId($question_id)->num_rows();
        if ($num_rows > 0) {
            // is not updatable
            echo 'err';
        } else {
            // is updatable
            $this->questionmodel->update_question($question_id, null, null, $question_content);
            echo 'succ';
        }
    }

    public function is_locked() {
        $this->layout = false;
        $question_id = $this->input->post('question_id');
        return $this->questionmodel->is_locked($question_id);
    }

    public function show_question() {
        // view is handled by javascript : positioning in list
        $this->layout = false;
        $question_id = $this->input->post('question_id');
        $this->questionmodel->update_question($question_id, null, 2, null);
        echo 'succ';
    }

    public function hide_question() {
        // view is handled by javascript : positioning in list
        $this->layout = false;
        $question_id = $this->input->post('question_id');
        $this->questionmodel->update_question($question_id, -99, 1, null);
        echo 'succ';
    }

    public function update_question_position() {
        // other view is handled by javascript : setting prev-id and next-id
        $this->layout = false;
        $question_id = $this->input->post('question_id');
        $new_parent_id = $this->input->post('new_parent_id');
        $this->questionmodel->update_question($question_id, $new_parent_id, null, null);
        echo 'succ';
    }

    /* END OF AJAX-RELATED FUNCTIONS */
}

/* End of file site.php */
/* Location: ./application/controllers/admin/site.php */
